﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WordRacer.Domain.Model.GameCells;

namespace WordRacer.Domain.Model.Boards
{
	public static class BoardHelper
	{
		public static IList<string> GetPaths(Board testBoard)
		{
			var ListOfPaths = new List<string>();
      string currentWord = string.Empty;

			foreach (var StarterCell in testBoard.GetBoardCells()) //each cell is a starting point.
			{
				if(StarterCell.Character.ToUpper() == "Q")
				{
					currentWord = "QU";
				}
        else
        {
					currentWord = StarterCell.Character;
				}
				StarterCell.InUse = true;

        var SetOfLetters = GetNextCell(StarterCell);
        foreach (var nextSetOfLetters in SetOfLetters)
        {
          ListOfPaths.Add(currentWord + nextSetOfLetters);  
        }

        StarterCell.InUse = false;
		 
			}
			return ListOfPaths;
		}

    private static IList<string> GetNextCell(GameCell currentCell)
    {
      IList<string> retPaths = new List<string>();
      string currentWord = string.Empty;
        //now that we have a starting point, we should move through all possible paths available to the game board from this starting point.

        //use currentCell as the starting point. 
        foreach (var nextCell in currentCell.ConnectedCells) 
        {
          if (!nextCell.InUse)
          {
            //add this character to the list, then move on to the next cell 
            if (nextCell.Character.ToUpper() == "Q")
            {
              currentWord = "QU";
            }
            else
            {
              currentWord = nextCell.Character;
            }
            retPaths.Add(currentWord);
            nextCell.InUse = true;
            var letterCollection = GetNextCell(nextCell);
            foreach (var nextSetOfLetters in letterCollection)
            {
              retPaths.Add(currentWord + nextSetOfLetters);
            }
            nextCell.InUse = false; 
          }
        }
        return retPaths;
    }
	}
}
